package leetcode.editor.vscdev;

import leetcode.editor.utils.*;

public class lc235 {
    /* 235. 二叉搜索树的最近公共祖先
     * 
     */
}
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null) return root;
        // 左
        if (root.val > p.val && root.val > q.val){
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            if (left != null) return left;
        }
        // 右
        if (root.val < p.val && root.val < q.val){
            TreeNode right = lowestCommonAncestor(root.right, p, q);
            if (right != null) return right;
        }
        return root;    
    }
}
